﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.UI;
using FwProject.Core.AppVersion;
using FwProject.ShoppingMall.Com;
using FwProject.ShoppingMall.EventAdvertising;

namespace FwProject.Manager
{
    public class ShopCarManager : IDomainService
    {
        private readonly IRepository<ShopCar, Guid> _shopCarRepository;

        public ShopCarManager(IRepository<ShopCar, Guid> shopCarRepository)
        {
            _shopCarRepository = shopCarRepository;
        }
        /// <summary>
        /// 添加一个购物车数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<Guid> CreateShopCar(ShopCar model)
        {
            var shopModel = _shopCarRepository.GetAll().Where(o => o.UserId == model.UserId && o.ShopproductId == model.ShopproductId && o.ShopSkuId==model.ShopSkuId).FirstOrDefault();
            if (shopModel != null)
            {
                shopModel.ProductCount += model.ProductCount;
                return shopModel.Id;
            }

            return await _shopCarRepository.InsertAndGetIdAsync(model);
        }
        /// <summary>
        /// 修改一个购物车
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>

        public async Task UpdateShopCar(ShopCar model)
        {
            await _shopCarRepository.UpdateAsync(model);
        }

        /// <summary>
        /// 删除一个购物车数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task DeleteShopCar(ShopCar model)
        {
            await _shopCarRepository.DeleteAsync(model);
        }
    }
}
